package com.sunny.lock;

import java.util.Queue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Test {

    private static final int NUM = 5;

    public static void main(String[] args) {

        ExecutorService producerTP = Executors.newFixedThreadPool(NUM);
        ExecutorService consumerTP = Executors.newFixedThreadPool(NUM);

        Queue<Integer> queue = new LinkedBlockingQueue<>();
        int max = 10;
        Lock lockProducer = new ReentrantLock();
        Lock lockConsumer = new ReentrantLock();
        Condition full = lockProducer.newCondition();
        Condition empty = lockConsumer.newCondition();

        for(int i=0;i<NUM;i++){
            Producer producer = new Producer(lockProducer, lockConsumer,full,empty,max,queue,"生产者"+i);
            producerTP.execute(producer);
        }
        for(int i=0;i<NUM;i++){
            Consumer consumer = new Consumer(lockProducer,lockConsumer,full,empty,max,queue,"消费者"+i);
            consumerTP.execute(consumer);
        }


    }

}
